package test;

import java.io.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TEST20200428 {

    /**使用Set集合特性判断int数组是否有重复值
     *
     * @param ins int数组
     * @return false 重复 true 不重复
     */
    public  static boolean isRe(int [] ins){
        Set<Integer> sets = new HashSet<>(ins.length);
        for (int i : ins) {
            sets.add(i);
        }
        return ins.length == sets.size();
    }

    /**使用ASCII码转换人名币大写
     *
     * @param s 需要转换的人名币
     * @return RMB大写
     */
    private String toRMB(String s){
        if(s == null || s.trim().length() <=0)return "";
        if(s.split("\\.").length>2||!s.replace(".","").matches("[0-9]+")){ return ""; }
        s = s.trim();
        String leftNum[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String unit[] = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"};
        String rightNum[] = {"分", "角"};
        String left = s,right = "";
        if(s.contains(".")){
            left = s.substring(0,s.indexOf("."));
            right = s.substring(s.indexOf(".")+1,s.length());
        }
        StringBuilder RMB = new StringBuilder();
        char [] lefts = left.toCharArray();
        for (int i = 0; i < lefts.length; i++) {
           RMB.append(leftNum[lefts[i]-48]).append(unit[left.length() -i -1]);
        }
        char [] rights = right.toCharArray();
        for (int i = 0; i < rights.length; i++) {
            RMB.append(leftNum[rights[i]-48]).append(rightNum[right.length() -i -1]);
        }
        return RMB.toString();
    }

    /**获取路径下文件的统计信息
     * @param filePath  文件路径
     * @return Map 通过key获取需要的信息
     * @throws IOException 文件处理异常
     */
    public Map<String,Integer> getFilePathCounts(String filePath) throws IOException {
        int charNums = 0,digitalNums = 0,spaceNums = 0,rowNums = 0;
        //字符正则
        String regEx1="[a-zA-z]";
        Pattern charPattern = Pattern.compile(regEx1);
        //数字正则
        String regEx2="[^0-9]";
        Pattern digitalPattern = Pattern.compile(regEx2);
        //空格正则
        String regEx3 = "\\t";
        Pattern spacePattern = Pattern.compile(regEx3);

        File dir = new File(filePath);
        if(dir.isDirectory()){
            File[] files = dir.listFiles();
            if(files != null){
                for (File file : files) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
                    String s = null;
                    while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                        rowNums += 1;//行数+1
                        //字母统计
                        Matcher charMatcher = charPattern.matcher(s);
                        charNums+=charMatcher.replaceAll("").trim().length();
                        //数字统计
                        Matcher digitalMatcher = digitalPattern.matcher(s);
                        digitalNums+=digitalMatcher.replaceAll("").trim().length();
                        //空格统计
                        Matcher spaceMatcher = spacePattern.matcher(s);
                        while (spaceMatcher.find()){spaceNums++;}
                    }
                    br.close();
                }
            }
        }

        Map<String,Integer> msg = new HashMap<>();
        msg.put("charNums",charNums);//字符个数
        msg.put("digitalNums",digitalNums);//数字个数
        msg.put("spaceNums",spaceNums);//空格数
        msg.put("rowNums",rowNums);//总行数
        return msg;
    }


}
